package com.tarena.lbs.basic.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class AreaRepositoryImpl implements AreaRepository {
    private final AreaMapper areaMapper;
    @Override
    public List<AreaVO> queryProvinces() {
        QueryWrapper<AreaPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<AreaPO> defaultList = areaMapper.selectList(queryWrapper);
        List<AreaVO> resultList = new ArrayList<>();
        defaultList.forEach(po -> {
            AreaVO vo = new AreaVO();
            BeanUtils.copyProperties(po,vo);
            resultList.add(vo);
        });
        return resultList.stream().sorted((Comparator.comparing(AreaVO::getId))).toList();
    }
    @Override
    public List<AreaVO> queryChildren(Integer parentId) {
        QueryWrapper<AreaPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<AreaPO> defaultList = areaMapper.selectList(queryWrapper);
        List<AreaVO> resultList = new ArrayList<>();
        defaultList.forEach(po -> {
            AreaVO vo = new AreaVO();
            BeanUtils.copyProperties(po,vo);
            resultList.add(vo);
        });
        return resultList.stream().sorted((Comparator.comparing(AreaVO::getId))).toList();
    }
}
